package com.douwong.student.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.douwong.student.R;
import com.douwong.student.base.BaseActivity;
import com.douwong.student.enums.E_SubjectType;
import com.douwong.student.popuwindow.ImageUrlShowPopu;
import com.douwong.student.popuwindow.ShowQuestionAnswerDetailPopuWindow;
import com.douwong.student.rxjava.EndPointInterface;
import com.douwong.student.utils.ImagFilterUtil;
import com.douwong.student.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 答题记录->问答互动
 *
 * @author yichao
 */
public class QuestioAnswerShowActivity extends BaseActivity {

    private static final String TAG = QuestioAnswerShowActivity.class.getSimpleName();
    private TextView tv_question, tv_Topic_type, tv_lastAnswer, tv_Correct_rate, tv_tip_title;
    private TextView tv_Judgment_right, tv_Judgment_wrong;
    private LinearLayout ll_Judgment_questions, ll_tip_box;
    private ImageView iv_answer_show_pic, iv_people;
    private ImageButton ib_submitButton;
    private String MTAG = "QuizShowActivity";


    private RelativeLayout rl_little;


    private ScrollView sv_options;
    private LinearLayout result_ll, title_ll;
    private String[] chart = {"A", "B", "C", "D", "E", "F", "G", "H"};
    private int[] linearlayouts = new int[]{R.id.ll_A, R.id.ll_B, R.id.ll_C, R.id.ll_D, R.id.ll_E, R.id.ll_F,
            R.id.ll_G, R.id.ll_H};
    private int[] textViews = new int[]{R.id.tv_choose_A, R.id.tv_choose_B, R.id.tv_choose_c, R.id.tv_choose_D,
            R.id.tv_choose_e, R.id.tv_choose_F, R.id.tv_choose_g, R.id.tv_choose_H};

    private int Topic_type;
    private View answerquestions_content = null;
    private E_SubjectType mE_subjectType;
    private String mStudentAnswer;
    private String mTrueAnswer;
    private String mTitleName;
    private String mFilePath;
    private String mOptionStr;
    private String mType;
    private ImageUrlShowPopu mImageUrlShowPopu;
    private List<String> mStringList = new ArrayList<>();
    private ShowQuestionAnswerDetailPopuWindow mShowQADetailPopuWindow;

    @Override
    protected void initView() {
        setContentView(R.layout.answerquestions);
        answerquestions_content = findViewById(R.id.answerquestions_content);

        findViewById(R.id.upload_ll).setVisibility(View.GONE);
        title_ll = (LinearLayout) findViewById(R.id.title_ll);
        result_ll = (LinearLayout) findViewById(R.id.result_ll);
        sv_options = (ScrollView) findViewById(R.id.sv_options);
        rl_little = (RelativeLayout) findViewById(R.id.rl_little);

        ll_tip_box = (LinearLayout) findViewById(R.id.tip_box);
        ll_Judgment_questions = (LinearLayout) findViewById(R.id.ll_Judgment_questions);
        tv_question = (TextView) findViewById(R.id.tv_question);
        tv_Judgment_right = (TextView) findViewById(R.id.tv_Judgment_right);
        tv_Judgment_wrong = (TextView) findViewById(R.id.tv_Judgment_wrong);
        tv_tip_title = (TextView) findViewById(R.id.tv_tip_title);

        tv_lastAnswer = (TextView) findViewById(R.id.tv_lastAnswer);
        tv_Correct_rate = (TextView) findViewById(R.id.tv_Correct_rate);

        ib_submitButton = (ImageButton) findViewById(R.id.ib_submitButton);
        tv_Topic_type = (TextView) findViewById(R.id.tv_Topic_type);
        iv_answer_show_pic = (ImageView) findViewById(R.id.iv_answer_show_pic);
        iv_people = (ImageView) findViewById(R.id.iv_people);
        ib_submitButton.setVisibility(View.INVISIBLE);


    }

    @Override
    protected void initData() {
        super.initData();
        answerquestions_content.setVisibility(View.VISIBLE);
        answerquestions_content.setBackgroundResource(R.mipmap.answerbg);

        Intent intent = getIntent();
        mTitleName = intent.getStringExtra("titleName");
        mFilePath = intent.getStringExtra("filePath");
        mOptionStr = intent.getStringExtra("optionStr");
        mStudentAnswer = intent.getStringExtra("studentAnswer");
        mTrueAnswer = intent.getStringExtra("trueAnswer");
        mType = intent.getStringExtra("type");
        result_ll.setVisibility(View.VISIBLE);
        tv_lastAnswer.setText("正确答案:" + mTrueAnswer);
        if (mType.equals("1")) {
            Topic_type = 1;
            mE_subjectType = E_SubjectType.single;
            showTextView(mOptionStr);
            tv_Topic_type.setBackgroundResource(R.mipmap.radiobuttonlistpic);
            RadioTreatment();
        } else if (mType.equals("2")) {
            Topic_type = 2;
            mE_subjectType = E_SubjectType.multiple;
            showTextView(mOptionStr);
            tv_Topic_type.setBackgroundResource(R.mipmap.multiple_choice_question_pic);
            MultipleProcessing();
        } else if (mType.equals("15")) {
            Topic_type = 0;
            mE_subjectType = E_SubjectType.judgment;
            for (int i = 0; i < chart.length; i++) {
                findViewById(linearlayouts[i]).setVisibility(View.GONE);
            }
            ll_Judgment_questions.setVisibility(View.VISIBLE);
            tv_Topic_type.setBackgroundResource(R.mipmap.judgment_questions_pic);
            tv_Judgment_right.setClickable(false);
            tv_Judgment_wrong.setClickable(false);

            JudgmentProcessing();
        }
        //设置标题

        ImagFilterUtil.filterImage(tv_question, mTitleName + "", 1);

        if (mFilePath == null || mFilePath.equals("")) {
            iv_answer_show_pic.setVisibility(View.GONE);
        } else {
            iv_answer_show_pic.setVisibility(View.VISIBLE);
            Glide.with(this).load(EndPointInterface.HEAD_PIC + mFilePath).placeholder(R.mipmap.answerdefault).into(iv_answer_show_pic);
        }

    }

    @Override
    protected void initEvent() {
        super.initEvent();
        iv_answer_show_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mImageUrlShowPopu = new ImageUrlShowPopu(QuestioAnswerShowActivity.this, mFilePath);
                mImageUrlShowPopu.showAtLocation(v, Gravity.CENTER, 0, 0);


            }
        });
        title_ll.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mStringList.clear();
                String[] split = mOptionStr.split(",");

                if (split != null && split.length > 0) {
                    for (int i = 0; i < split.length; i++) {
                        mStringList.add(split[i]);
                    }
                }
                mShowQADetailPopuWindow = new ShowQuestionAnswerDetailPopuWindow(QuestioAnswerShowActivity.this, mE_subjectType, mTitleName, mStringList, mFilePath);
                //ShowAnswerDetailPopuWindow popu = new ShowAnswerDetailPopuWindow(QuizShowActivity.this, Topic_type + "",
                //		"result", OptionStr, TitleName);
                LogUtils.i(TAG, " 问答记录   +++ 问答互动 ： " + mOptionStr);
                mShowQADetailPopuWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
            }
        });
    }

    /**
     * @param context
     * @param titleName     题目
     * @param filePath      图片路径
     * @param optionStr     选项
     * @param studentAnswer 学生答案
     * @param trueAnswer    正确答案
     * @param type          题目类型
     */
    public static void startActivity(Context context, String titleName, String filePath,
                                     String optionStr, String studentAnswer,
                                     String trueAnswer, String type) {
        Intent intent = new Intent(context, QuestioAnswerShowActivity.class);
        intent.putExtra("titleName", titleName);
        intent.putExtra("filePath", filePath);
        intent.putExtra("optionStr", optionStr);
        intent.putExtra("studentAnswer", studentAnswer);
        intent.putExtra("trueAnswer", trueAnswer);
        intent.putExtra("type", type);
        context.startActivity(intent);


    }


    /**
     * 显示控件
     *
     * @param OptionStr
     */
    public void showTextView(String OptionStr) {
        String Str[] = OptionStr.split(",");
        if (Str != null && Str.length > 0) {
            //给每个选项赋值
            for (int i = 0; i < Str.length; i++) {
                findViewById(linearlayouts[i]).setVisibility(View.VISIBLE);
                TextView text = (TextView) findViewById(textViews[i]);
                String selectValue = Str[i];
                //	text.setText(selectValue.substring(2,selectValue.length()-1));

                LogUtils.i(TAG, " l问答互动 选项答案 ：" + selectValue);
                ImagFilterUtil.filterImage(text, selectValue.substring(2, selectValue.length()), 1);
            }
        }


    }

    // 判断处理
    public void JudgmentProcessing() {

        if (!mStudentAnswer.equals("")) {
            if (mStudentAnswer.equals(mTrueAnswer)) {

                iv_people.setBackgroundResource(R.mipmap.answerright);
            } else {

                iv_people.setBackgroundResource(R.mipmap.answerwrong_people);
            }
            if (mStudentAnswer.equalsIgnoreCase("Yes")) {
                tv_Judgment_right.setBackgroundResource(R.mipmap.answer_right_r);
            } else if (mStudentAnswer.equalsIgnoreCase("No")) {
                tv_Judgment_wrong.setBackgroundResource(R.mipmap.answer_wrong_r);
            }

            tv_tip_title.setBackgroundResource(R.mipmap.prompt_w);
        } else {
            iv_people.setBackgroundResource(R.mipmap.answerwrong_people);
        }

    }

    private void showResult(String str, int id) {
        for (int i = 0; i < chart.length; i++) {
            if (str.equals(chart[i])) {
                findViewById(linearlayouts[i]).setBackgroundResource(id);
                break;
            }
        }
    }

    // 单选处理
    public void RadioTreatment() {
        iv_people.setBackgroundResource(R.mipmap.answerright);
        tv_tip_title.setBackgroundResource(R.mipmap.prompt_r);
        Log.i(MTAG, "singleChoice result : " + mStudentAnswer);
        showResult(mStudentAnswer, R.mipmap.option_box_r);
        if (!mStudentAnswer.equals(mTrueAnswer)) {
            tv_tip_title.setBackgroundResource(R.mipmap.prompt_w);
            iv_people.setBackgroundResource(R.mipmap.answerwrong_people);
        }
    }

    // 多选处理
    public void MultipleProcessing() {
        tv_tip_title.setBackgroundResource(R.mipmap.prompt_r);
        iv_people.setBackgroundResource(R.mipmap.answerright);


        if (mStudentAnswer.length() > 0) {
            if (mStudentAnswer.length() == 1) {
                //只选1ge
                showResult(mStudentAnswer, R.mipmap.option_box_r);

            } else {

                String[] StrStudent = mStudentAnswer.split(",");
                for (int i = 0; i < StrStudent.length; i++) {

                    showResult(StrStudent[i], R.mipmap.option_box_r);
                }
            }
        }


        if (!mStudentAnswer.equals(mTrueAnswer)) {
            tv_tip_title.setBackgroundResource(R.mipmap.prompt_w);
            iv_people.setBackgroundResource(R.mipmap.answerwrong_people);
        }

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (mImageUrlShowPopu != null && mImageUrlShowPopu.isShowing()) {
            mImageUrlShowPopu.dismiss();
        }
        if (mShowQADetailPopuWindow != null && mShowQADetailPopuWindow.isShowing()) {
            mShowQADetailPopuWindow.dismiss();
        }
        System.gc();
        try {

            //ViewGroup 背景 子类会抛异常
            BitmapDrawable bitmap = (BitmapDrawable) answerquestions_content.getBackground();
            answerquestions_content.setBackgroundResource(0);
            if (bitmap != null) {
                bitmap.setCallback(null);
                //				bitmap.getBitmap().recycle();
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }


}
